package com.example.myapplication2;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

//懒惰权限
public class PermissionsLazyActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String[] PERMISSIONS_CONTACTS = new String[]{
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.WRITE_CONTACTS
    };
    private static final String[] PERMISSIONS_SMS = new String[]{
            Manifest.permission.READ_SMS,
            Manifest.permission.SEND_SMS
    };
    private static final int REQUEST_CODE_CONTACTS = 1;
    private static final int REQUEST_CODE_SMS = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permissions);
        findViewById(R.id.SMS).setOnClickListener(this);
        findViewById(R.id.contacts).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.SMS:
                checkPermissions(this, PERMISSIONS_SMS, REQUEST_CODE_SMS);
                break;
            case R.id.contacts:
                checkPermissions(this, PERMISSIONS_CONTACTS, REQUEST_CODE_CONTACTS);
                break;
        }
    }

    //检查多个权限,返回true表示已完全启用权限,返回false表示未完全启用权限
    private boolean checkPermissions(Activity act, String[] permissions, int requestCode) {
        //判定该系统是否拥有动态权限管理,Android6.0之后才有该功能
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int check = PackageManager.PERMISSION_GRANTED;
            for (String permission : permissions) {
                // 检查权限,返回值为-1,即为无相关权限,0即拥有相关权限
                check = ContextCompat.checkSelfPermission(act, permission);
                //判定权限
                if (check != PackageManager.PERMISSION_GRANTED) {
                    //无相关权限,结束检查
                    break;
                }
            }
            if (check != PackageManager.PERMISSION_GRANTED) {
                //检查到未申请该权限,弹窗申请
                ActivityCompat.requestPermissions(act, permissions, requestCode);
                return false;
            }
        }
        Toast.makeText(this,"权限检查完毕,权限正常",Toast.LENGTH_SHORT).show();
        return true;
    }

    private boolean checkGrant(int[] grantResults) {
        if (grantResults != null) {
            for (int grantResult : grantResults) {
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    private void jumpToSettings() {
        Intent intent = new Intent();
        //应用的详情设置界面
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        //具体某个应用管理页面
        intent.setData(Uri.fromParts("package",getPackageName(),null));
        //跳转应用模式
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    //重写请求权限结果方法,获取授权情况
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //查询用户判定结果
        switch (requestCode) {
            case REQUEST_CODE_CONTACTS:
                if (checkGrant(grantResults)) {
                    Log.d("info", "授权成功");
                } else {
                    Log.d("info", "授权失败");
                    jumpToSettings();
                }
                break;
            case REQUEST_CODE_SMS:
                if (checkGrant(grantResults)) {
                    Log.d("info", "授权成功");
                } else {
                    Log.d("info", "授权失败");
                    jumpToSettings();
                }
                break;
        }
    }
}